//
//  SubjectEditView.m
//  
//
//  Created by Christian Speich on 18.12.07.
//  Copyright 2007-2008 Christian Speich. All rights reserved.
//

#import "SubjectEditView.h"
#import "SubjectsManager.h"
#import "Mark.h"
#import <UIKit/UIPreferencesTableCell.h>
#import <UIKit/UIPreferencesTextTableCell.h>


@implementation SubjectEditView

- (id)initNewTestWithSubjectIndex:(int)i andFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self != nil) {
		index = i;
		classTest = false;
		table = [[UIPreferencesTable alloc] 
				 initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, 
										  frame.size.height)];
		[table setDataSource:self];
		[table setDelegate:self];
		[table reloadData];
		
		[self addSubview:table];
	}
	return self;
}

- (id)initNewClassTestWithSubjectIndex:(int)i andFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self != nil) {
		index = i;
		classTest = true;
		table = [[UIPreferencesTable alloc] 
				 initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, 
										  frame.size.height)];
		[table setDataSource:self];
		[table setDelegate:self];
		[table reloadData];
		
		[self addSubview:table];
	}
	return self;
}

- (void)save
{
	Mark *mark = [[Mark alloc] init];
	
	[mark setMark:[markControl selectedSegment]+1];
	
	if (classTest) {
		[mark setType:@"classTest"];
		
		switch ([wertigkeitControl selectedSegment]) {
			case 0:
				[mark setValency:0.2f];
				break;
			case 1:
				[mark setValency:0.25f];
				break;
			default:
				break;
		}
		[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
		 addMark:mark];
	}
	else {
		[mark setType:@"test"];
		[[[SubjectsManager sharedInstance] subjectAtIndex:index] addMark:mark];
	}
	//[[SubjectsManager sharedInstance] writeFile];
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 1;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	if (classTest) {
		return 2;
	}
	return 1;
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table 
								cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table 
								  cellForRow:(int)row inGroup:(int)group
{
	if (group == 0) {
		if (row == 0) {
			UIPreferencesTableCell *cell;
			cell = [[UIPreferencesTableCell alloc] init];
			markControl = [[UISegmentedControl alloc] 
						   initWithFrame:CGRectMake(120.0f, 5.0f, 180.0f, 
													55.0f)];
			[markControl insertSegment:0 withTitle:@"1" animated: NO];
			[markControl insertSegment:1 withTitle:@"2" animated: NO];
			[markControl insertSegment:2 withTitle:@"3" animated: NO];
			[markControl insertSegment:3 withTitle:@"4" animated: NO];
			[markControl insertSegment:4 withTitle:@"5" animated: NO];
			[markControl insertSegment:5 withTitle:@"6" animated: NO];
			[cell setTitle:@"Note"];
			[cell addSubview:markControl];
			return [cell autorelease];

		}
		else if (row == 1) {
			UIPreferencesTableCell *cell;
			cell = [[UIPreferencesTableCell alloc] init];
			wertigkeitControl = [[UISegmentedControl alloc] 
								 initWithFrame:CGRectMake(120.0f, 5.0f, 180.0f, 
														  55.0f)];
			[wertigkeitControl insertSegment:0 withTitle:@"20%" animated: NO];
			[wertigkeitControl insertSegment:1 withTitle:@"25%" animated: NO];
			[cell setTitle:@"Wertigkeit"];
			[cell addSubview:wertigkeitControl];
			return [cell autorelease];

		}
	}
}

- (float)preferencesTable:(UIPreferencesTable *)aTable 
    heightForRow:(int)row 
    inGroup:(int)group 
    withProposedHeight:(float)proposed 
{
    if (group == 0) {
        switch (row) {
            case 0 :
			case 1:
                return 55;
                break;
        }
    }
    return proposed;
}

- (void)tableRowSelected:(NSNotification*)notification
{
	[[table cellAtRow:[table selectedRow] column:0] setSelected:NO withFade:NO];
}

- (void)becomesManager:(id)manager
{
	viewManager = (ViewManager*)manager;
}

- (UINavigationItem*)NavigationItem
{
	return [[UINavigationItem alloc] 
			initWithTitle:classTest?NSLocalizedString(@"newClassTest",
													  @"Label for new Class Test")
			:NSLocalizedString(@"newTest",
							   @"Label for new Test")];
}

- (void)navigationBar:(UINavigationBar*)_bar buttonClicked:(int)button
{
	if (button == 1) {
		[[viewManager bar] popNavigationItem];
		return;
	}
	else {
		[self save];
		[[viewManager bar] popNavigationItem];
	}
}

- (void)showBarButtons
{
	[[viewManager bar] showLeftButton:nil withStyle:0 
						  rightButton:NSLocalizedString(@"save",
														@"Label for Save") 
							withStyle:3];	

}

@end
